package com.crm.kz.menu;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.crm.kz.R;
import com.crm.kz.common.BaseFragment;
import com.crm.kz.common.GlobalVariable;
import com.xuexiang.xui.widget.imageview.RadiusImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuFragment  extends BaseFragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.menu_fragment, container, false);
        titleBar.setVisibility(View.GONE);
        int width = GlobalVariable.width;
        Log.d("TAG", "onCreate: width"+width);
        GridLayout gridLayout = view.findViewById(R.id.gridMenu);
        View.OnClickListener onclick = new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                menuOnclick(view);
            }
        };
        List<Map<String,Object>> menu = getMenuList();
        for (Map<String,Object> item : menu) {
            LinearLayout layout = new LinearLayout(mContext);
            gridLayout.addView(layout);
            ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
//            layoutParams.width = (width - 200) / 4;
            ViewGroup.MarginLayoutParams mar = (ViewGroup.MarginLayoutParams) layout.getLayoutParams();
            mar.setMargins(25,50,25,0);
            layout.setLayoutParams(layoutParams);
            layout.setOrientation(LinearLayout.VERTICAL);

            LinearLayout layout2 = new LinearLayout(mContext);
            layout.addView(layout2);

            ViewGroup.LayoutParams layout2Params = layout2.getLayoutParams();
            layout2Params.width = (width - 200) / 4;
            layout2Params.height = (width - 200) / 4;
            layout2.setLayoutParams(layout2Params);
            layout2.setGravity(Gravity.CENTER);
            layout2.setBackground(mContext.getDrawable(R.drawable.circular_white));

            RadiusImageView riv = new RadiusImageView(mContext);
            riv.setSelectedMaskColor(mContext.getColor(com.xuexiang.xui.R.color.xui_config_color_gray_8));
            riv.setSelectedBorderColor(mContext.getColor(com.xuexiang.xui.R.color.xui_config_color_gray_4));
            layout2.addView(riv);
            riv.setId((Integer) item.get("id"));
            riv.setOnClickListener(onclick);
            ViewGroup.LayoutParams rivParams = riv.getLayoutParams();
            riv.setCircle(true);
            rivParams.width = (width - 200) / 5;
            rivParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            riv.setLayoutParams(rivParams);
            riv.setImageResource((Integer) item.get("img"));

            riv.setCircle(true);
            riv.setClickable(true);


            TextView tv = new TextView(mContext);
            layout.addView(tv);
            tv.setText((String)item.get("text"));
            tv.setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);

        }
        return view;
    }


    private void menuOnclick(View view){
        RadiusImageView riv = (RadiusImageView) view;

        switch (riv.getId()){


            case 1:
                gotoFragment(Fragment1.newInstance());
                break;
            case 2:
                gotoFragment(Fragment2.newInstance());
                break;
            case 3:
                gotoFragment(Fragment3.newInstance());
                break;
            case 6:
                gotoFragment(new LoginFragment());
                break;
        }
    }
    private List<Map<String,Object>> getMenuList(){
        List<Map<String,Object>>menu = new ArrayList<>();
        Map<String,Object>jh = new HashMap<>();
        jh.put("img",R.mipmap.jihua2);
        jh.put("text","工作计划");
        jh.put("id",1);
        menu.add(jh);

        Map<String,Object>wc = new HashMap<>();
        wc.put("img",R.mipmap.waichu);
        wc.put("text","外出访客");
        wc.put("id",2);
        menu.add(wc);

        Map<String,Object>rb = new HashMap<>();
        rb.put("img",R.mipmap.ribao);
        rb.put("text","员工流水");
        rb.put("id",3);
        menu.add(rb);

        Map<String,Object>khjl = new HashMap<>();
        khjl.put("img",R.mipmap.kehujilu);
        khjl.put("text","客户记录");
        khjl.put("id",4);
        menu.add(khjl);

        Map<String,Object>hyjl = new HashMap<>();
        hyjl.put("img",R.mipmap.huiyi);
        hyjl.put("text","会议记录");
        hyjl.put("id",5);
        menu.add(hyjl);

        Map<String,Object>system = new HashMap<>();
        system.put("img",R.mipmap.shezhi);
        system.put("text","设置");
        system.put("id",6);
        menu.add(system);

        return menu;
    }

    public void gotoFragment(BaseFragment fragment){
        titleBar.setVisibility(View.VISIBLE);
        FragmentManager fm = getActivity().getSupportFragmentManager();
        fm.beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static MenuFragment newInstance() {
        MenuFragment menuFragment = new MenuFragment();
        return menuFragment;
    }
}
